﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChoWorkflowServer.ModelSqlCE
{
    internal class ChoDataModelSql
    {
        public static readonly string Cho_IS_TASK_EXISTS = "SELECT DELETED FROM Cho_TASKS WHERE TASK_NAME = @TASK_NAME";
        public static readonly string Cho_ADD_OR_UPDATE_TASK = @"MERGE Cho_TASKS AS t
                                            USING (SELECT TASK_ID FROM Cho_TASKS WHERE TASK_ID = @TASK_ID) AS td ON td.TASK_ID = t.TASK_ID
                                            WHEN MATCHED THEN 
                                                UPDATE SET TASK_NAME = @TASK_NAME, WORKFLOW_NAME = @WORKFLOW_NAME, TASK_DESC = @TASK_DESC, TASK_GRP_ID = @TASK_GRP_ID, CMD_PARAMETERS = @CMD_PARAMETERS, NO_OF_RETRY = @NO_OF_RETRY, SLEEP_BETWEEN_RETRY = @SLEEP_BETWEEN_RETRY, TIMEOUT = @TIMEOUT, MACHINE_NAME = @MACHINE_NAME, USER_NAME = @USER_NAME
                                            WHEN NOT MATCHED THEN
                                                INSERT (TASK_NAME, WORKFLOW_NAME, TASK_DESC, TASK_GRP_ID, CMD_PARAMETERS, NO_OF_RETRY, SLEEP_BETWEEN_RETRY, TIMEOUT, MACHINE_NAME, USER_NAME)
                                                    VALUES(@TASK_NAME, @WORKFLOW_NAME, @TASK_DESC, @TASK_GRP_ID, @CMD_PARAMETERS, @NO_OF_RETRY, @SLEEP_BETWEEN_RETRY, @TIMEOUT, @MACHINE_NAME, @USER_NAME)";
        public static readonly string Cho_ADD_OR_UPDATE_TASK_SCHEDULES = @"MERGE Cho_TASK_SCHEDULES AS t
                                            USING (SELECT SCHEDULE_ID FROM Cho_TASK_SCHEDULES WHERE SCHEDULE_ID = @SCHEDULE_ID) AS td ON td.SCHEDULE_ID = t.SCHEDULE_ID
                                            WHEN MATCHED THEN 
		                                        UPDATE Cho_TASK_SCHEDULES
			                                        SET 
				                                        SCHEDULE_NAME = @SCHEDULE_NAME,
				                                        SCHEDULE_DESC = @SCHEDULE_DESC,
				                                        TASK_ID = @TASK_ID,
				                                        [ENABLED] = @ENABLED,
				                                        START_DATE = @START_DATE,
				                                        END_DATE = @END_DATE,
				                                        START_TIME = @START_TIME,
				                                        END_TIME = @END_TIME,
				                                        HOLIDAY_CAL_ID = @HOLIDAY_CAL_ID,
				                                        SCHEDULE_TYPE_ID = @SCHEDULE_TYPE_ID,
				                                        T1 = @T1,
				                                        DAILY_EVERY_DAYS = @DAILY_EVERY_DAYS,
				                                        WEEKLY_EVERY_WEEKS = @WEEKLY_EVERY_WEEKS,
				                                        SUN = @SUN,
				                                        MON = @MON,
				                                        TUE = @TUE,
				                                        WED = @WED,
				                                        THU = @THU,
				                                        FRI = @FRI,
				                                        SAT = @SAT,
				                                        MONTHLY_DAY_OF_MONTH = @MONTHLY_DAY_OF_MONTH,
				                                        MONTHLY_EVERY_DAY_OF_MONTH = @MONTHLY_EVERY_DAY_OF_MONTH,
				                                        MONTHLY_EVERY_WEEK_OF_MONTH = @MONTHLY_EVERY_WEEK_OF_MONTH,
				                                        MONTHLY_EVERY_WEEKDAY_OF_MONTH = @MONTHLY_EVERY_WEEKDAY_OF_MONTH,
				                                        MONTHLY_SELECTED_MONTHS = @MONTHLY_SELECTED_MONTHS,
				                                        REPEAT_ENABLED = @REPEAT_ENABLED,
				                                        REPEAT_INTERVAL = @REPEAT_INTERVAL,
				                                        REPEAT_INTERVAL_TYPE = @REPEAT_INTERVAL_TYPE,
				                                        DEPENDENCIES = @DEPENDENCIES,
				                                        FIRE_AFTER_DEPENDENCY = @FIRE_AFTER_DEPENDENCY,
				                                        MODIFIED_BY = @MODIFIED_BY,
				                                        MODIFIED_DATE = GETDATE()
                                            WHEN NOT MATCHED THEN
                                                INSERT (TASK_NAME, WORKFLOW_NAME, TASK_DESC, TASK_GRP_ID, CMD_PARAMETERS, NO_OF_RETRY, SLEEP_BETWEEN_RETRY, TIMEOUT, MACHINE_NAME, USER_NAME)
                                                    VALUES(@TASK_NAME, @WORKFLOW_NAME, @TASK_DESC, @TASK_GRP_ID, @CMD_PARAMETERS, @NO_OF_RETRY, @SLEEP_BETWEEN_RETRY, @TIMEOUT, @MACHINE_NAME, @USER_NAME)";
    }
}
